package com.youfang.sixthmedia.chat.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;

import com.gzit.common.async.AsynCallback;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.message.chat.GetUnReadMsgCntDelegate;
import com.youfang.sixthmedia.net.Net;
import com.youfang.sixthmedia.net.Response;

public class UNReadMessageMonitorService extends Service {

	public static final String TAG = "UNReadMessageService";

	@Override
	public IBinder onBind(Intent arg0) {

		return null;
	}

	boolean running = false;

	Thread fetchingMessageThread = new Thread() {

		public void run() {

			while (running) {

				if (YoufangContext.isLogined()) {
					fetchMessageCount();
				}

				try {
					Thread.sleep(YoufangContext.MESSAGE_POLL_INTERVAL);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}

		}

	};

	private void fetchMessageCount() {

		if (!YoufangContext.isLogined()) {
			return;
		}

		final GetUnReadMsgCntDelegate d = new GetUnReadMsgCntDelegate();

		d.req.userid = YoufangContext.getUser().userid;

		Net.simpleMessage(d, new AsynCallback<Response>() {

			@Override
			public void callback(Response r) {

				if (r.isOk()) {
					notifyMessageCntFeteched(d);
				}

			}
		});

	}

	private void notifyMessageCntFeteched(GetUnReadMsgCntDelegate delegate) {

		// got message count ,send notification out...
		Intent intent = new Intent();
		intent.setAction(YoufangContext.CHAT_UNREAD_MESSAGE_ATION);
		intent.putExtra("userid", delegate.req.userid);
		intent.putExtra("data", delegate.resp.count);
		this.sendBroadcast(intent);

	}

	@Override
	public void onCreate() {
		super.onCreate();

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			String action = bundle.getString("action");
			if ("start".equals(action)) {
				if (!running && !fetchingMessageThread.isAlive()) {
					running = true;
					fetchingMessageThread.setName("message count monitor");
					fetchingMessageThread.start();
				}

			} else if ("stop".equals(action)) {
				running = false;
				this.stopSelf();
			}

		}

		return super.onStartCommand(intent, flags, startId);

	}

	public static void start(Context ctx) {

		Intent intent = new Intent();
		intent.setAction(YoufangContext.CHAT_UNREAD_MESSAGE_ATION);
		intent.putExtra("action", "start");
		intent.setClass(ctx, UNReadMessageMonitorService.class);
		ctx.startService(intent);

	}

	public static void stop(Context ctx) {

		Intent intent = new Intent();
		intent.setAction(YoufangContext.CHAT_UNREAD_MESSAGE_ATION);
		intent.putExtra("action", "stop");
		intent.setClass(ctx, UNReadMessageMonitorService.class);
		ctx.startService(intent);

	}

}
